package com.cm.sys.framework.mybatiesinterceptor;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**java中的boolean和jdbc中的char之间转换;true-Y;false-N*/
public class BooleanTypeHandle implements TypeHandler {
    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        System.out.println("进入了自定义返回类型转换器传参数");
        Boolean flag=(Boolean)parameter;
        if(flag==true){
            ps.setString(i,"Y");
        }else{
            ps.setString(i, "N");
        }
    }

    @Override
    public Object getResult(ResultSet rs, String columnName) throws SQLException {
        System.out.println("进入了自定义返回类型转换器");
        String str = rs.getString(columnName);
        Boolean rt = Boolean.FALSE;
        if ("Y".equalsIgnoreCase(str)){
            rt = Boolean.TRUE;
        }else{
            rt = Boolean.FALSE;
        }
        return rt;
    }

    @Override
    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        return null;
    }


    @Override
    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
